﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.UI.WebControls;

namespace QuanLyVanBanBUS
{
    public class TinhChatBUS
    {
        public void themTinhChat(TinhChat lvbDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            storeDB.TinhChats.InsertOnSubmit(lvbDTO);

            storeDB.SubmitChanges();
        }

        public void suaTinhChat(TinhChat lvbDTO, string maTinhChat)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.TinhChats.Where(q => q.MaTinhChat == maTinhChat).First();
            query.TenTinhChat = lvbDTO.TenTinhChat;

            storeDB.SubmitChanges();
        }

        public void xoaTinhChat(GridView gridView)
        {
            //Lay MaTinhChat duoc chon tu CheckBox
            string temp = string.Empty;

            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox _checkBox = (CheckBox)(gridView.Rows[i].Cells[0].FindControl("chkMaTinhChat"));

                if (_checkBox.Checked == true)
                {
                    string maTinhChatCanXoa = gridView.DataKeys[i].Value.ToString();

                    temp += maTinhChatCanXoa + ",";
                }
            }

            //Kiem tra co check vao CheckBox khong?
            if (temp.Length - 1 > 0)
            {
                temp = temp.Substring(0, temp.Length - 1);
            }
            else
            {
                //Response.Write("<script>alert('Xin vui lòng chọn Sản phẩm cần xóa')</script>");

                return;
            }

            //Xoa
            QLVBDataContext storeDB = new QLVBDataContext();

            //Them cac MaTinhChat duoc chon vao mang
            string[] m_temp = new string[] { temp };

            while (temp.Length > 0)
            {
                //Lay maTemp de lam dieu kien so sanh. Lay tu sau lay toi cua Mang
                string maTemp = temp.Substring(temp.LastIndexOf(",") + 1, temp.Length - (temp.LastIndexOf(",") + 1));

                var query = storeDB.TinhChats.Where(q => q.MaTinhChat == maTemp).First();

                storeDB.TinhChats.DeleteOnSubmit(query);

                storeDB.SubmitChanges();

                //Kiem tra dieu kien co con trong vong lap While khong?
                if ((temp.Length - (maTemp.Length + 1)) > 0)
                {
                    temp = temp.Substring(0, temp.Length - (maTemp.Length + 1));
                }
                else
                    break;
            }

            gridView.DataSource = layDanhSachTinhChat();
            gridView.DataBind();
        }

        public IList<TinhChat> layDanhSachTinhChat()
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var lstTinhChat = storeDB.TinhChats.ToList();

            return lstTinhChat;
        }

        public Boolean kiemtraTonTaiMaTinhChat(string maTinhChat)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.TinhChats.Where(q => q.MaTinhChat == maTinhChat).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public Boolean kiemtraTonTaiTenTinhChat(string tenTinhChat)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.TinhChats.Where(q => q.TenTinhChat == tenTinhChat).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public string layMaTinhChat(string maTinhChat)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var _maTinhChat = storeDB.TinhChats.Where(m => m.MaTinhChat == maTinhChat).Select(m => m.MaTinhChat).First();

            return _maTinhChat;
        }

        public string layTenTinhChat(string maTinhChat)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var tenTinhChat = storeDB.TinhChats.Where(m => m.MaTinhChat == maTinhChat).Select(m => m.TenTinhChat).First();

            return tenTinhChat;
        }

        public void themItemVaoDropDownList(DropDownList dropDownList)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.TinhChats.ToList();
            foreach (var q in query)
            {
                var maTinhChat = q.MaTinhChat;
                var tenTinhChat = q.TenTinhChat;

                ListItem item = new ListItem();
                item.Text = tenTinhChat;
                item.Value = maTinhChat;

                dropDownList.Items.Add(item);
            }
        }
    }
}
